// pages/aftersalebox/aftersalecreate/aftersalecreate.js
import {
  getAftersaleInfo,
  buyersubaftersale,
  getToken
} from '../../../utils/networkRequest.js'
import {
  errorMsg,
} from '../../../utils/tool.js'
import {
  _upImgsUrl
} from '../../../utils/config.js'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataInfo: null,
    images: [],
    id: "",
    expno: "",
    token:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;

    // 登录 
    wx.login({
      success: lr => {
        getToken(lr.code, (tr) => {
          if (tr.code == 0) {
            getAftersaleInfo(options.id,tr.data.token, (r) => {
              if (r.code == 0) {
                if (r.data.im_seller && r.data.status != 0) {
                  wx.redirectTo({
                    url: '../aftersaledetailview/aftersaledetailview?id=' + r.data.id,
                  })
                } else {
                  self.setData({
                    dataInfo: r.data,
                    images: r.data.pics,
                    id: options.id,
                    token:tr.data.token

                  })
                }

              }

            })

          } else {
            errorMsg(r.message);
          }
        })
      }
    })



  },
  //选择图片
  checkPhoto: function() {
    let {
      id,
    } = this.data;
    let self = this;
    if (self.data.images.length < 5) {
      wx.chooseImage({
        count: (5 - self.data.images.length),
        sizeType: ['original'],
        sourceType: ['album', 'camera'],
        success(res) {
          const images = res.tempFilePaths
          self.data.images = self.data.images.concat(images);
          self.setData({
            images: self.data.images
          })
        }
      })
    } else {
      errorMsg("最多只能选择5张照片")
    }

  },
  onclose: function(e) {
    let self = this;
    let index = e.currentTarget.dataset.index;
    self.data.images.map((o, i) => {
      if (i == index) {
        self.data.images.splice(i, 1)
        self.setData({
          images: self.data.images
        })
      }
    })
  },
  //扫一扫获取二维码
  onScanCode: function() {
    wx.scanCode({
      success: (res) => {
        this.setData({
          expno: res.result
        })
      },
      fail: (res) => {
        errorMsg("识别失败")
      }
    })
  },
  aftersalesubmit: function(e) {
    let {
      id,
      images,
      token
    } = this.data;
    let {
      remark,
      expno,
      name,
      mobile,
      address,
    } = e.detail.value;




    // let token2 = null;
    // try {
    //   var value = wx.getStorageSync('token') //从本地缓存中获取Session信息 
    //   if (value) {
    //     token2 = value.token;
    //   }
    // } catch (e) {

    // }

    let defaluteHeader = {
      'content-type': 'application/json',
    }

    if (token) { //如果token存在把他放入头部
      defaluteHeader = {
        'content-type': 'application/json',
        'Authorization': "Bearer " + token,
      }
    }

    if (remark == "") {
      errorMsg("请输入问题描述")
    } else if (expno == "") {
      errorMsg("请输入快递单号")
    } else if (name == "") {
      errorMsg("请输入您的姓名")
    } else if (mobile == "") {
      errorMsg("请输入您的手机号")
    } else {

      if (images[0]) {
        app.uploadimg({
          url: _upImgsUrl, //这里是你图片上传的接口
          path: images, //这里是选取的图片的地址数组  
          header: defaluteHeader,
          prams: {
            id: id
          },
          name: 'images',
        }, function() {
          //图片上传完之后 提交表单  
            buyersubaftersale(id, remark, name, mobile, address, expno, e.detail.formId, token, (r) => {
            if (r.code == 0) {
              wx.redirectTo({
                url: '../aftersalesubsuccess/aftersalesubsuccess',
              })
            } else {
              errorMsg(r.message)
            }
          })
        }.bind(this))
      } else {
        buyersubaftersale(id, remark, name, mobile, address, expno, e.detail.formId, token, (r) => {
          if (r.code == 0) {
            wx.redirectTo({
              url: '../aftersalesubsuccess/aftersalesubsuccess',
            })
          } else {
            errorMsg(r.message)
          }
        })
      }


    }
  }
})